<?php

/**
 *  订单
*/

namespace app\api\controller;
use app\api\service\Order as ServiceOrder;
use app\admin\service\Order as ServiceAdminOrder;
use app\facade\Basic;
use think\Request;

class Order extends BaseController
{

    /**
     *  订单结算
    */
    public function checkout(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['method'] = $request->param('method','cart');
        $param['product'] = $request->param('product',null);
        $param['lang'] = $request->header('think-lang');
        // 如果需要同时获取多个IP信息
        $param['ip'] = getRealIp($request);

        /* 测试数据 */
//        $param['ip'] = '182.239.127.137';

        /* 验证 */
        $this->validateData($param,'Order.checkout');

        $ServiceCarts = new ServiceOrder();
        $result = $ServiceCarts->checkout($param);
        return Basic::json($result);
    }


    /**
     *  统一下单接口
    */
    public function create(Request $request)
    {

        $param['user_id'] = $request->user_id;
        $param['role'] = $request->role == 'user' ? 1 : 2;
        $param['origin'] = $request->param('origin',1);  // 下单类型  直接下单和购物车
        $param['ids'] = $request->param('ids',0);  // 下单类型  直接下单和购物车
        $param['product'] = $request->param('product',[]);
        $param['is_wholesal'] = $request->param('is_wholesal',1);
        $param['address_id'] = $request->param('address_id',null);
        $param['billing_address_id'] = $request->param('billing_address_id',null);
        $param['payment_method'] = $request->param('payment_method',null);
        $param['warehouse_code'] = $request->param('warehouse_code',null);

        $param['delivery_method'] = $request->param('delivery_method',null);  // 收货方式(0-自提 1-快递)
        $param['delivery_terms'] = $request->param('delivery_terms',null);   // 贸易条款(1-DDP 2-DDU)
        $param['prescription'] = $request->param('prescription',null);     // 1普快/2特快

        /* 支付方式 */
        $param['pay_terms'] = $request->param('pay_terms',0);   // 默认值 0全款 1预付款
        $param['advance_amount'] = $request->param('advance_amount',0.00);  // 预付款金额

        /* 自提 */
        $param['pickup_name'] = $request->param('pickup_name',null);  // 自提名称
        $param['pickup_phone'] = $request->param('pickup_phone',null);  // 自提电话
        $param['pickup_passport'] = $request->param('pickup_passport',null);  // 护照
        $param['pickup_vehicle'] = $request->param('pickup_vehicle',null);  // 车辆信息

        /* 优惠券 */
        $param['discount_code'] = $request->param('discount_code',null);  // 优惠券码
        /* 备注 */
         $param['notes'] = $request->param('notes',null);  // 订单备注

        if ( $param['is_wholesal'] == 2 ) {
            /* 经销商下单验证 */
            $this->validateData($param,'Order.wholesal');
        } else {
            /* 普通订单验证 */
            $this->validateData($param,'Order.order');

            /* 自提信息 */
            if( $param['delivery_method'] == 0 ) {
                $this->validateData($param,'Order.pickup');
            }
        }

        $ServiceCarts = new ServiceOrder();
        $result = $ServiceCarts->create($param);
        return Basic::json($result);
    }


    /**
     *  订单列表
    */
    public function list(Request $request)
    {

        $param['user_id'] = $request->user_id;
        $param['role'] = $request->role;
        $param['lang'] = $request->header('think-lang');

        /* 页面 */
        $param['page'] = $request->param('page',1);
        $param['listRows'] = $request->param('listRows',15);

        /* 筛选 */
        $param['status'] = $request->param('status',0);

        /* 搜索 */
        $param['search_value'] = $request->param('search_value',null);

        /* 验证 */
        $this->validateData($param,'order.list');

        /* 登录操作 */
        $ServiceDistributorOrder = new ServiceOrder();
        $result = $ServiceDistributorOrder->list($param);
        return Basic::json($result);

    }

    /**
     *  订单详情
    */
    public function detail(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['role'] = $request->role;
        $param['orderNo'] = $request->param('orderNo',null);
        $param['lang'] = $request->header('think-lang');

        /* 验证 */
        $this->validateData($param,'order.detail');

        /* 登录操作 */
        $ServiceDistributorOrder = new ServiceOrder();
        $result = $ServiceDistributorOrder->detail($param);
        return Basic::json($result);

    }


    /**
     * 上传支付凭证
     */
    public function PaymentVoucher(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['role'] = $request->role;
        $param['orderNo'] = $request->param('orderNo',null);
        $param['file'] = $request->file('file');
        $param['notes'] = $request->param('notes',null);

        /* 验证 */
        $this->validateData($param,'order.payment_voucher');

        /* 登录操作 */
        $ServiceDistributorOrder = new ServiceOrder();
        $result = $ServiceDistributorOrder->PaymentVoucher($param);
        return Basic::json($result);
    }

    /**
     *  删除删除凭证
    */
    public function VoucherRemove(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['role'] = $request->role;
        $param['id'] = $request->param('id',null);

        /* 验证 */
        $this->validateData($param,'order.voucher_remove');

        /* 登录操作 */
        $ServiceDistributorOrder = new ServiceOrder();
        $result = $ServiceDistributorOrder->VoucherRemove($param);
        return Basic::json($result);
    }


    /**
     *  拉起支付
    */
    public function payment(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['role'] = $request->role;
        $param['orderNo'] = $request->param('orderNo',null);
        $param['payment_method'] = $request->param('payment_method',null);
        $param['paid_amount'] = $request->param('paid_amount',null);  // 付款金额
        $param['voucher_code'] = $request->param('voucher_code',null);  // 付款凭证吗
        $param['file'] = $request->file('file');
        $param['voucher_remark'] = $request->param('voucher_remark',null);

        /* 验证 */
        $this->validateData($param,'order.payment');

        /* 登录操作 */
        $ServiceDistributorOrder = new ServiceOrder();
        $result = $ServiceDistributorOrder->payment($param);
        return Basic::json($result);
    }

    /**
     *  订单取消
     */
    public function cancel(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['orderNo'] = $request->param('orderNo',null);
        /* 验证 */
        $this->validateData($param,'order.cancel');

        /* 订单取消 */
        $ServiceOrder = new ServiceOrder();
        $result = $ServiceOrder->cancel($param);
        return Basic::json($result);
    }

    /**
     *  订单取消
     */
    public function calculatePrice(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['productid'] = $request->param('productid',0);
        $param['wid'] = $request->param('wid',0);
        $param['arrival_date'] = $request->param('arrival_date',null);
        $param['is_futures'] = $request->param('is_futures',null);
        $param['quantity'] = $request->param('quantity',null);
        /* 验证 */
        $this->validateData($param,'order.calculatePrice');

        /* 订单取消 */
        $ServiceOrder = new ServiceOrder();
        $result = $ServiceOrder->calculatePrice($param);
        return Basic::json($result);
    }
    /**
     *  订单状态列表
    */
    public function getStatusList(Request $request)
    {
        $lang = $request->header('think-lang');
        $ServiceDistributorOrder = new ServiceOrder();
        $result = $ServiceDistributorOrder->getStatusList($lang);
        return Basic::json($result);
    }



    /**
     *  修改订单地址
     *  @param Request $request
    */
    public function setAddress(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['scene'] = $request->param('scene',1);
        $param['address_id'] = $request->param('address_id',null);
        $param['orderNo'] = $request->param('orderNo',null);

        $ServiceOrder = new ServiceOrder();
        $result = $ServiceOrder->setAddress($param);
        return Basic::json($result);

    }

    /**
     *  合同预览
     */
    public function contractPreview(Request $request){
        $id  = $request->param('id',null);

        $ServiceOrder = new ServiceAdminOrder();
        $result = $ServiceOrder->contractPreview($id,2);
        return Basic::json($result);
    }


}
